import React, {
	Component
} from 'react';
export default class VerificationCode extends Component {
	constructor(props) {
		super(props);
		this.state = {
			date: new Date()
		}
		this.imgStyle = {
			cursor: 'pointer',
		};
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.date !== nextProps.date) {
			this.setState({
				date: new Date()
			});
		}
	}

	handleClick = () => {
		this.setState({
			date: new Date()
		});
	}

	translateObjToStr = (extra) => {
		let str = '';

		Object.keys(extra).forEach(key => {
			str += str + `${key}=${extra[key]}`
		});

		return str;
	}

	render() {
		const {
			date
		} = this.state;

		const {
			extra
		} = this.props;

		const str = extra ? `&${this.translateObjToStr(extra)}` : '';

		return (
			<img 
                src={`/weaver/weaver.file.MakeValidateCode?isView=1&validatetype=0&validatenum=4${str}&seriesnum_=${date}`}
                style={this.imgStyle} 
                onClick={() => this.handleClick() }
            />
		);
	}
}